# coding=utf-8
from settings import *
from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from psp.models import *

"""
copyright (c) 2009, paketka@gmail.com et. al
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

	* Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.

	* Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.

	* Neither the name of the <ORGANIZATION> nor the names of its contributors
	  may be used to endorse or promote products derived from this software
 	  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""


class PersonSitemap(Sitemap):
	changefreq = "monthly"

	def items(self):
		return Person.objects.all()

	def lastmod(self, obj):
		mships = Membership.objects.filter(person = obj).order_by('-start')

		if mships.count() == 0:
			return datetime.date(year = 1993, month = 1, day = 1)

		lastDate = mships[0].start

		mships = Membership.objects.filter(person = obj).order_by('-end')
		
		if lastDate != None and (mships[0].end == None or lastDate > mships[0].end):
			return lastDate
		else:
			if mships[0].end != None:
				return mships[0].end
			else:
				return datetime.date(year = 1993, month = 1, day = 1)

	def location(self, obj):
		return reverse('psp.views.show_mp', args = [obj.slug])

	def priority(self, obj):
		mships = Membership.objects.filter(person = obj).order_by('-start')
		if mships.count() == 0:
			return 0.0

		maxId = TermOfOffice.objects.order_by('-id')[0].id
		maxId += 1
		maxId -= mships[0].group.term.id

		return 0.8 / maxId
		

class PollsSitemap(Sitemap):
	changefreq = "daily"

	def __init__(self):
		super( Sitemap, self ).__init__()
		self.recentDate = Poll.objects.order_by('-date')[0].date
	
	def items(self):
		return Poll.objects.all()

	def lastmod(self, obj):
		return obj.date

	def location(self, obj):
		return reverse('psp.views.show_poll', args = [ obj.id ])

	def priority(self, obj):
		delta = self.recentDate - obj.date

		return 0.9 * (1.0 / (delta.days + 1))


class GroupSitemap(Sitemap):
	changefreq = "weekly"

	def items(self):
		return Group.objects.all()

	def lastmod(self, obj):
		mships = Membership.objects.filter(group = obj).order_by('-start')

		if mships.count() == 0:
			return datetime.date(year = 1993, month = 1, day = 1)

		lastDate = mships[0].start

		mships = Membership.objects.filter(group = obj).order_by('-end')
		
		if lastDate != None and (mships[0].end == None or lastDate > mships[0].end):
			return lastDate
		else:
			if mships[0].end != None:
				return mships[0].end
			else:
				return datetime.date(year = 1993, month = 1, day = 1)

	def location(self, obj):
		return reverse('psp.views.show_group', args = [ obj.slug ])


	def priority(self, obj):
		maxId = TermOfOffice.objects.order_by('-id')[0].id
		maxId += 1
		maxId -= obj.term.id

		return 0.85 / maxId


def robots(request):
	robots = 'User-agent:*\nDisallow:/json/\n'
	robots += 'Disallow:/justice/\n'
	return HttpResponse(robots, mimetype = 'text/plain')

sitemaps = {
	'poslanci' : PersonSitemap,
	'hlasovani' : PollsSitemap,
	'skupiny' : GroupSitemap,
}
